package com.longdaji.decoration.ui.activitiesOfMine.pendingEvaluated;

import android.content.Context;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.TextView;

import com.longdaji.decoration.Constants;
import com.longdaji.decoration.R;
import com.longdaji.decoration.adapter.DetailProductAdapter;
import com.longdaji.decoration.base.BaseActivity;
import com.longdaji.decoration.bean.Orders;
import com.longdaji.decoration.ui.activitiesOfMine.refund.forRefund.ForRefundActivity;
import com.longdaji.decoration.utils.LogUtil;

import java.util.ArrayList;
import java.util.List;

import javax.inject.Inject;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class OrderDetailActivity extends BaseActivity implements OrderDetailContract.IView {

    private static final String TAG = "OrderDetailActivity";

    @Inject
    OrderDetailContract.IPresenter mPresenter;
    @BindView(R.id.tv_tool_title)
    TextView tvTitle;
    @BindView(R.id.tv_contact_people)
    TextView tvContactPeople;
    @BindView(R.id.tv_telephone)
    TextView tvTelephone;
    @BindView(R.id.tv_address)
    TextView tvAddress;
    @BindView(R.id.tv_signer)
    TextView tvSigner;
    @BindView(R.id.tv_end_time)
    TextView tvEndTime;
    @BindView(R.id.tv_goods_count)
    TextView tvGoodsCount;
    @BindView(R.id.tv_custom)
    TextView tvCustom;
    @BindView(R.id.rv_product)
    RecyclerView rvProduct;
    @BindView(R.id.tv_money_one)
    TextView tvMoneyOne;
    @BindView(R.id.tv_money_two)
    TextView tvMoneyTwo;
    @BindView(R.id.tv_money_three)
    TextView tvMoneyThree;
    @BindView(R.id.tv_money_end)
    TextView tvMoneyEnd;
    @BindView(R.id.tv_pay_time)
    TextView tvPayTime;
    @BindView(R.id.tv_pay_way)
    TextView tvPayWay;
    @BindView(R.id.tv_order_no)
    TextView tvOrderNo;
    @BindView(R.id.tv_status)
    TextView tvOrderStatus;

    private String orderNo;
    private List<Orders.OrdersBean.OrderBean.ProductsBean> productList = new ArrayList<>();
    private DetailProductAdapter mAdapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_order_detail);

        setUnBinder(ButterKnife.bind(this));
        if (mPresenter != null)
            mPresenter.setView(this);
        tvTitle.setText("订单详情");

        initView();
    }

    public static void start(Context context, Intent extras) {
        Intent intent = new Intent(context, OrderDetailActivity.class);
        if (extras != null) {
            intent.putExtras(extras);
        }
        context.startActivity(intent);
    }

    @Override
    protected void onNewIntent(Intent intent) {
        super.onNewIntent(intent);
        LogUtil.d(TAG);
        setIntent(intent);
        mAdapter.notifyDataSetChanged();

    }

    private void initView() {
        orderNo = getIntent().getStringExtra(Constants.ORDER_NO);
        mPresenter.initOrder(orderNo);
        mAdapter = new DetailProductAdapter(productList);
        mAdapter.setOnItemClickListener(new DetailProductAdapter.OnItemClickListener() {
            @Override
            public void onApplyRefundClick(int goodsId) {
                Intent intent = new Intent(OrderDetailActivity.this, ForRefundActivity.class);
                intent.putExtra(Constants.ORDER_NO, orderNo);
                intent.putExtra(Constants.GOODS_ID, goodsId);
                startActivity(intent);
            }

            @Override
            public void onEvaluateClick(int goodsId) {
                Intent intent = new Intent(OrderDetailActivity.this, EvaluateActivity.class);
                intent.putExtra(Constants.ORDER_NO, orderNo);
                intent.putExtra(Constants.GOODS_ID, goodsId);
                startActivity(intent);
            }
        });
        rvProduct.setLayoutManager(new LinearLayoutManager(this));
        rvProduct.setAdapter(mAdapter);
    }


    @Override
    public void onDestroy() {
        super.onDestroy();
        if (mPresenter != null)
            mPresenter.detachView();
    }

    @OnClick({R.id.rl_tool_back})
    void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.rl_tool_back:
                finish();
                break;
            default:
                break;
        }
    }

    @Override
    public void showInitOrder(Orders.OrdersBean.OrderBean order) {
        tvContactPeople.setText(order.getShippingAddress().getContactPerson());
        tvTelephone.setText(order.getShippingAddress().getContactTelephone());
        tvAddress.setText(order.getShippingAddress().getDetailAddress());
        tvSigner.setText("已签收，签收者是" + order.getShippingAddress().getContactPerson());
        // TODO: 2018/5/13 0013 endTime
        tvEndTime.setText(String.valueOf(order.getTakeGoodDate()));
        // TODO: 2018/5/13 0013 价格怎么计算，运费是哪一条
        tvMoneyOne.setText("$" + order.getProductAmountTotal() + "");
        tvMoneyTwo.setText("$");
        tvMoneyThree.setText("$");
        tvMoneyEnd.setText("$" + order.getOrderAmountTotal() + "");
        tvPayTime.setText("下单时间: " + order.getPayDate());
        tvPayWay.setText("支付方式: " + order.getOrderPayChannel());
        tvOrderNo.setText("订单编号: " + orderNo);

        LogUtil.d("mytest", "products: " + order.getProducts().size());
        mAdapter.setData(order.getProducts());
    }
}
